package com.haiyou.sdk.thirdSdk;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson2.JSONObject;
import com.haiyou.common.constants.ErrorCode;
import com.haiyou.common.exception.ServiceException;
import com.haiyou.common.system.Properties;
import com.haiyou.common.util.check.Assert;
import com.haiyou.common.util.http.HttpUtils;
import com.haiyou.common.util.string.StringFormatUitls;
import com.haiyou.common.util.time.ServerTime;
import com.haiyou.protocol.http.sdk.login.AuthRequest;
import com.haiyou.protocol.http.sdk.thirdSdk.AiWanUserAuthResponse;
import com.qcloud.cos.utils.Md5Utils;

import lombok.extern.slf4j.Slf4j;

/**
 * 爱玩SDK
 * 
 * @author xingyuan
 * @date 2023年12月6日 上午9:32:09
 */
@Slf4j
@Component
public class AiWanSdkService {

	public static final String user_auth_format = "{}?account={}&app_id={}&time={}&token={}&sign={}";

	public static final String login_sign_format = "account={}&app_id={}&time={}&token={}{}";
	
	/**
	 * 正常
	 */
	public static final int code_200 = 200;

	
	/**
	 * 登录验证
	 * @param request
	 * @return uid 
	 */
	public String authSign(AuthRequest request) {
		
		// https://api-sdk.essmicrogames.com/user/auth
		
		String url = Properties.sdk_aiwan_user_auth_url();
		
		Assert.isTrue(StringUtils.hasLength(url), ErrorCode.ERROR_CONFIG,"爱玩登录sdk验证配置错误");
		
		Assert.isTrue(StringUtils.hasLength(request.getToken()), ErrorCode.ERROR_PARAMETER,"爱玩登录参数错误");
		Assert.isTrue(StringUtils.hasLength(request.getUid()), ErrorCode.ERROR_PARAMETER,"爱玩登录参数错误");

		//https://api-sdk.essmicrogames.com/user/auth?account=2020092315165768
		//&app_id=ovdqy3we3nekjrz8&time=1600845363
		//&token=NwVe8n4zMizM1Mzg3MzZfMTYwMDg0NTM2M184MDkz
		//&sign=0aea0c2c60bb6988f83d8a8023906af5
		
		String account = request.getUid();
		String app_id = Properties.sdk_aiwan_app_id();
		
		Assert.isTrue(StringUtils.hasLength(app_id), ErrorCode.ERROR_CONFIG,"爱玩app_id 配置错误");
		long time = ServerTime.timeSecond();
		String token = request.getToken();
		String sign = "";
		String app_key = Properties.sdk_aiwan_app_key();
		
		Assert.isTrue(StringUtils.hasLength(app_key), ErrorCode.ERROR_CONFIG,"爱玩app_key 配置错误");

		String toSign = StringFormatUitls.format(login_sign_format, account,app_id,time,token,app_key);
		
		log.info("爱玩 待签名内容:{}",toSign);
		
		//签名
		sign = Md5Utils.md5Hex(toSign);
		
		log.info("爱玩 签名结果:{}",sign);

		
		//"{}?account={}&app_id={}&time={}&token={}&sign={}";
		url = StringFormatUitls.format(user_auth_format, url,account,app_id,time,token,sign);
		
		log.info("爱玩 登录url:{}",url);
		
		try {
			
			String content = HttpUtils.getThrowException(url);
			
			log.info("爱玩 登录验证返回参数 {}",content);
			
			AiWanUserAuthResponse response = JSONObject.parseObject(content, AiWanUserAuthResponse.class);

			Assert.isTrue(response.getCode() == code_200, ErrorCode.ERROR_PARAMETER,"sdk验证错误  {}",content);

			//sdk 的uid
			String sdkUid = response.getData().getString("account");
			
			Assert.isTrue(StringUtils.hasLength(sdkUid), ErrorCode.ERROR_PARAMETER,"sdk验证错误  {}",content);
			
			return sdkUid;
			
		} catch (ServiceException e) {
			throw e;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		
		Assert.isTrue(false, ErrorCode.ERROR_PARAMETER,"sdk验证错误");
		
		return "";
	}
	
	public static void main(String[] args) {
		
		
		//https://api-sdk.essmicrogames.com/user/auth?account=2020092315165768
		//&app_id=ovdqy3we3nekjrz8&time=1600845363
		//&token=NwVe8n4zMizM1Mzg3MzZfMTYwMDg0NTM2M184MDkz
		//&sign=0aea0c2c60bb6988f83d8a8023906af5
				
		
		String account = "2020092315165768";
		String app_id = "ovdqy3we3nekjrz8";
		
		
		long time = 1600845363l;
		String token = "NwVe8n4zMizM1Mzg3MzZfMTYwMDg0NTM2M184MDkz";
		String sign = "";
		String app_key = Properties.sdk_aiwan_app_key();
		String toSign = StringFormatUitls.format(login_sign_format, account,app_id,time,token,app_key);

	}
}
